<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 17/12/20
 * Time: 16:21
 */

Yaf_Loader::import(CONTROLLERS_PATH.'Controller.php');
Yaf_Loader::import(EXTEND_PATH.'/qvideo/Videos.php');
Yaf_Loader::import(EXTEND_PATH.'/qmessage/Messages.php');

class MeetingController extends Controller
{
    protected $sdkAppID = 1400043256;
    protected $accountType = 17845;

    public function init()
    {
        $this->authentication = $this->authentication();
        if(!$this->authentication)
        {
            $this->redirect('/');
            exit;
        }
        $this->meeting = new MeetingModel();
        $this->getView()->assign("user", $this->authentication['data']);
        $this->group = new GroupModel();
        $group = $this->group->listsAction($this->authentication['data']);
        $this->getView()->assign("group", $group);
    }

    public function liveListsAction()
    {
        $page=$this->getRequest()->get("page");
        $user = $this->authentication['data'];
        $meeting = $this->meeting->liveListsAction($user['fid'],$page);
        $this->getView()->assign("meeting", $meeting);
        $this->getView()->assign("title", '会议列表');
    }

    public function liveAction()
    {
        $mid=$this->getRequest()->get("mid");
        $live = $this->meeting->liveAction($this->authentication['data'],$mid);
        if($live){
            $videos = new Videos();
            $video['url'] = $videos->getPlayUrl($live['channel'].'_m_'.$live['udid']);
            $video['sdkAppID'] = $this->sdkAppID;
            $video['accountType'] = $this->accountType;
            $video['nickname'] = $this->authentication['data']['nickname'];
            $video['udid'] = $this->authentication['data']['id'];
            $video['avatar'] = $this->authentication['data']['avatar'];
            $video['tim'] = $this->authentication['tim'];
            $video['channel'] = $live['channel'];
            $message = new Messages();
            $info = $message->get_group_member_info([$live['channel']]);
            $online = $this->onlineAction($mid,$info);
            $this->getView()->assign("online", $online);
            $this->meeting->addWatchAction($mid,$this->authentication['data'],1);
            $this->getView()->assign("video", $video);
        }else{
            $this->redirect('/Meeting/livelists');
        }
        $this->getView()->assign("live", $live);
        $this->getView()->assign("title", '观看会议');
    }

    public function backListsAction()
    {
        $page=$this->getRequest()->get("page");
        $user = $this->authentication['data'];
        $back = $this->meeting->backListsAction($user['fid'],$page);
        $this->getView()->assign("back", $back);
        $this->getView()->assign("title", '往期培训');
    }

    public function backAction()
    {
        $mid=$this->getRequest()->get("mid");
        $play = $this->meeting->backAction($this->authentication['data'],$mid);
        if($play){
            $videos = new Videos();
            $video['url'] = $videos->getVideoInfo($play['video']);
            $video['sdkAppID'] = $this->sdkAppID;
            $video['accountType'] = $this->accountType;
            $video['nickname'] = $this->authentication['data']['nickname'];
            $video['udid'] = $this->authentication['data']['id'];
            $video['avatar'] = $this->authentication['data']['avatar'];
            $video['tim'] = $this->authentication['tim'];
            $video['channel'] = $play['channel'];
            $this->meeting->addWatchAction($mid,$this->authentication['data'],9);
            $this->getView()->assign("video", $video);
        }else{
            $this->redirect('/Meeting/backlists');
        }
        $this->getView()->assign("play", $play);
        $this->getView()->assign("title", '回放');
    }

    public function passwordAction()
    {
        if($this->getRequest()->isPost()){
            $content = $this->getRequest()->getPost();
            $validate = new Validate([
                'mid' => 'require|number|egt:1|exist:meeting,id',
                'password' => 'require|number|length:6|between:100000,999999',
            ]);
            $validate->message([
                'mid.require' => '会议不能为空',
                'mid.number' => '会议类型错误',
                'mid.egt' => '会议错误',
                'mid.exist' => '会议不存在',
                'password.require' => '密码不能为空',
                'password.number' => '密码错误',
                'password.length' => '密码错误',
                'password.between' => '密码错误',
            ]);
            if ($validate->check($content)) {
                if($this->meeting->passwordAction($content,$this->authentication['data'])){
                    $code = 200;
                    $data['message'] = '验证通过';
                }else{
                    $code = 400;
                    $data['message'] = '密码错误';
                }
            }else{
                $code = 400;
                $data['message'] = $validate->getError();
            }
        }else{
            $code = 500;
            $data['message'] = '请求错误';
            $content = $this->getRequest()->getRequest();
        }
        $this->returnApi($data,$code,$content);
    }

    public function delAction()
    {
        if($this->getRequest()->isPost()){
            $content = $this->getRequest()->getPost();
            $validate = new Validate([
                'mid' => 'require|number|egt:1|exist:meeting,id',
            ]);
            $validate->message([
                'mid.require' => '培训不能为空',
                'mid.number' => '培训类型错误',
                'mid.egt' => '培训错误',
                'mid.exist' => '培训不存在',
            ]);
            if ($validate->check($content)) {
                if($this->authentication['data']['type']){
                    $mid = $content['mid'];
                    $data = $this->meeting->delAction($this->authentication['data'],$mid);
                    $code = 200;
                }else{
                    $code = 400;
                    $data['message'] = '权限不足,只有管理员可操作';
                }
            }else{
                $code = 400;
                $data['message'] = $validate->getError();
            }
        }else{
            $code = 500;
            $data['message'] = '请求错误';
            $content = $this->getRequest()->getRequest();
        }
        $this->returnApi($data,$code,$content);
    }

    private function onlineAction($mid,$info,$number=PAGE_NUMBER)
    {
        $user['lists'] = [];
        $user['page'] = 0;
        $user['count'] = 0;
        if(!empty($info['MemberNum'])){
            $users = $member = [];
            $detail = false;
            $i=$j=0;
            foreach ($info['MemberList'] as$k=> $v){
                if($v['Member_Account'] < 100000000){
                    if($i>=$number){
                        $j++;
                        $i=0;
                    }
                    $udid[$j][] = $v['Member_Account'];
                    $detail = true;
                    $i++;
                }else{
                    $member[$k]['udid']=$v['Member_Account'];
                    $member[$k]['nickname']='学员';
                    $member[$k]['avatar']='http://train.img.livestaring.com/log.png';
                }
            }
            if($detail){
                foreach ($udid as $v){
                    $user = $this->meeting->usersAction($v,$mid);
                    $users = array_merge($users,$user);
                }
            }
            $user['lists'] = array_merge($member,$users);
            $user['count'] = $info['MemberNum'];
        }
        return $user;
    }
}